﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data.SqlClient;
using Estimate.Common.DBFieldTypes;

namespace Estimate.Common.Repository
{
    /// <summary>
    /// 考核表数据访问数    
    /// </summary>
    public class ZytMonthScoreRepository : BaseRepository
    {

        public ZytMonthScoreRepository(string connectionString) : base(connectionString,"ZytMonthScore")
        {
        }

        public override string GetCreateTableSql()
        {
            string sqlcreate = String.Format("CREATE TABLE [{0}] (id bigint identity(1,1), startdate date,enddate date,type varchar(6),realtype varchar(8), sendpoints varchar(50), stations varchar(1000), scorestring text) \n", _tableName);
            sqlcreate += String.Format("CREATE CLUSTERED INDEX [{0}_INDEX] ON [{1}] (startdate,enddate,type,realtype,sendpoints,stations) \n", _tableName, _tableName);
            sqlcreate += String.Format("alter table [{0}] add primary key (id)", _tableName);
            return sqlcreate;
        }

        

        /// <summary>
        /// 查看考核表是否存在
        /// </summary>
        /// <param name="startDate">开始日期</param>
        /// <param name="endDate">结束日期</param>
        /// <param name="type">表类型</param>
        /// <param name="realtype">实况类型</param>
        /// <param name="sendpoints">发报时刻数组</param>
        /// <returns>是否存在相应记录</returns>
        public bool IsRecordExist(DateTime startDate, DateTime endDate, TableType type, ForeSemiType realtype, ForeSendpointType[] sendpoints, string stations)
        {
            string sql = String.Format("SELECT id FROM [{0}] WHERE startdate=@startdate  and enddate=@enddate and type=@type and realtype=@realtype and sendpoints=@sendpoints and stations=@stations", _tableName);
            SqlParameter[] param = new SqlParameter[6];
            param[0] = new SqlParameter("@startdate", startDate.ToString("yyyyMMdd"));
            param[1] = new SqlParameter("@enddate", endDate.ToString("yyyyMMdd"));
            param[2] = new SqlParameter("@type", type.ToString());
            param[3] = new SqlParameter("@realtype", realtype.ToString());
            param[4] = new SqlParameter("@sendpoints", getSortedSendpointsString(sendpoints));
            param[5] = new SqlParameter("@stations", this.getSortedStrings(stations) );
            return DBHelper.Exists(_connectionString, sql, param);
        }

        /// <summary>
        /// 获取月度报表的字符串值
        /// </summary>
        /// <param name="startDate">开始日期</param>
        /// <param name="endDate">结束日期</param>
        /// <param name="type">表类型</param>
        /// <param name="realtype">实况类型</param>
        /// <param name="sendpoints">发报时刻数组</param>
        /// <returns>存储的报表记录</returns>
        public string GetScoreString(DateTime startDate, DateTime endDate, TableType type, ForeSemiType realtype, ForeSendpointType[] sendpoints, string stations)
        {
            string sql = String.Format("SELECT scorestring FROM [{0}] WHERE startdate=@startdate  and enddate=@enddate and type=@type and realtype=@realtype and sendpoints=@sendpoints and stations=@stations", _tableName);
            SqlParameter[] param = new SqlParameter[6];
            param[0] = new SqlParameter("@startdate", startDate.ToString("yyyyMMdd"));
            param[1] = new SqlParameter("@enddate", endDate.ToString("yyyyMMdd"));
            param[2] = new SqlParameter("@type", type.ToString());
            param[3] = new SqlParameter("@realtype", realtype.ToString());
            param[4] = new SqlParameter("@sendpoints", getSortedSendpointsString(sendpoints));
            param[5] = new SqlParameter("@stations", this.getSortedStrings(stations));
            SqlConnection connection = new SqlConnection(_connectionString);
            SqlDataReader reader = DBHelper.ExecuteReader(connection, sql, param);
            string scorestring = "";
            if (reader.Read())
                scorestring = reader["scorestring"].ToString();
            connection.Close();
            return scorestring;
        }

        public void InsertScoreString(DateTime startDate, DateTime endDate, TableType type, ForeSemiType realtype, ForeSendpointType[] sendpoints, string stations, string scoreString)
        {
            string sql = String.Format("INSERT INTO [{0}] (startdate,enddate,type,realtype,sendpoints,stations,scorestring) values(@startdate ,@enddate ,@type,@realtype,@sendpoints,@stations,@scorestring)", _tableName);
            SqlParameter[] param = new SqlParameter[7];
            param[0] = new SqlParameter("@startdate", startDate.ToString("yyyyMMdd"));
            param[1] = new SqlParameter("@enddate", endDate.ToString("yyyyMMdd"));
            param[2] = new SqlParameter("@type", type.ToString());
            param[3] = new SqlParameter("@realtype", realtype.ToString());
            param[4] = new SqlParameter("@sendpoints", getSortedSendpointsString(sendpoints));
            param[5] = new SqlParameter("@stations", this.getSortedStrings(stations));
            param[6] = new SqlParameter("@scorestring", scoreString);
            DBHelper.ExecuteSql(_connectionString, sql, param);
        }

        public void UpdateScoreString(DateTime startDate, DateTime endDate, TableType type, ForeSemiType realtype, ForeSendpointType[] sendpoints, string stations, string scoreString)
        {
            string sql = String.Format("UPDATE [{0}] SET scorestring=@scorestring WHERE startdate=@startdate  and enddate=@enddate and type=@type and realtype=@realtype and sendpoints=@sendpoints and stations=@stations", _tableName);
            SqlParameter[] param = new SqlParameter[7];
            param[0] = new SqlParameter("@startdate", startDate.ToString("yyyyMMdd"));
            param[1] = new SqlParameter("@enddate", endDate.ToString("yyyyMMdd"));
            param[2] = new SqlParameter("@type", type.ToString());
            param[3] = new SqlParameter("@realtype", realtype.ToString());
            param[4] = new SqlParameter("@sendpoints", getSortedSendpointsString(sendpoints));
            param[5] = new SqlParameter("@stations", this.getSortedStrings(stations));
            param[6] = new SqlParameter("@scorestring", scoreString);
            DBHelper.ExecuteSql(_connectionString, sql, param);
        }

        public void SetScoreString(DateTime startDate, DateTime endDate, TableType type, ForeSemiType realtype, ForeSendpointType[] sendpoints, string stations, string scorestring)
        {
            if (IsRecordExist(startDate, endDate,type,realtype,sendpoints,stations))
                UpdateScoreString(startDate, endDate, type, realtype, sendpoints, stations, scorestring);
            else
                InsertScoreString(startDate, endDate, type, realtype, sendpoints,stations,scorestring);
           
        }
    }
}
